<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2017/3/28
 * Time: 11:05
 */
namespace app\common\model;
use think\Db;
class User extends \think\Model{
    public static $user_status = array(
        '1' => '正常',
        '2' => '冻结',
    );
    public static $sex = array(
        '1' => '男',
        '2' => '女',
    );
    public static $user_attestation = array(
        '1' => '未认证',
        '2' => '已认证',
        '3' => '认证中',
        '4' => '认证失败'
    );
    protected $table='up_u_user';

    /***
     * @param $pagesize
     * @param string $search
     * @return \think\Paginator
     * 首页列表数据
     */
    public function relData($pagesize,$search=''){
       $model = self::instance();
        $where = '';
        if($search[0]!=''){
            $where['u.user_status'] = ['=',$search[0]];
        }
        if($search[1]!=''){
            $where['u.user_gender'] = ['=',$search[1]];
        }
        if($search[2]!=''){
            $where['u.user_attestation'] = ['=',$search[2]];
        }
        if($search[3]!=''){
            $where['u.user_username|u.user_phone']=['like',"%$search[3]%"];
        }
        if($search[4]!=''){
            $where['u.user_province'] =['=',$search[4]];
        }

        if($search[5]!=''){
            $where['u.user_city'] =['=',$search[5]];
        }

        if($search[6]!=''){
            $where['u.user_area'] =['=',$search[6]];
        }

        if($search[7] != '' && $search[8] !=''){
            $where['u.user_time'] = array('between',array($search[7],$search[8]));
        }else if($search[7] != ''){
            $where['u.user_time'] = array('EGT',$search[7]);
        }else if($search[8]!=''){
            $where['u.user_time'] = array('ELT',$search[8]);
        }else{
            $where['u.user_time'] = array('EGT',0);
        }
       $list = $model->alias('u')
                     ->join('up_area ua1','ua1.district_id = u.user_province','left')
                     ->join('up_area ua2','ua2.district_id = u.user_city','left')
                     ->join('up_area ua3','ua3.district_id = u.user_area','left')
                     ->where($where)
                     ->order('u.user_id desc')
                     ->field('u.*,ua1.district as province,ua2.district as city,ua3.district as district')
                     ->paginate($pagesize);
        foreach ($list as $key=>&$vo){
            $dogCount = Db::table('up_u_user_interest_dog')->where('user_id',$vo['user_id'])->count();
            $dogCount = $dogCount?$dogCount:0;
            $userCount = Db::table('up_u_user_interest_user')->where('uu_userid',$vo['user_id'])->count();
            $userCount = $userCount?$userCount:0;
            $vo['dogCount'] = $dogCount;
            $vo['userCount'] = $userCount;
        }
       return $list;
   }

    /**
     * @param string $search
     * @return \think\Paginator
     */
    public function excelData($search=''){
        $model = self::instance();
        $where = '';
        if($search[0]!=''){
            $where['u.user_status'] = ['=',$search[0]];
        }
        if($search[1]!=''){
            $where['u.user_gender'] = ['=',$search[1]];
        }

        if($search[3]!=''){
            $where['u.user_username|u.user_phone']=['like',"%$search[3]%"];
        }
        if($search[4]!=''){
            $where['u.user_province'] =['=',$search[4]];
        }

        if($search[5]!=''){
            $where['u.user_city'] =['=',$search[5]];
        }
        if($search[6]!=''){
            $where['u.user_area'] =['=',$search[6]];
        }
        if($search[7] != '' && $search[8] !=''){
            $where['u.user_time'] = array('between',array($search[7],$search[8]));
        }else if($search[7] != ''){
            $where['u.user_time'] = array('EGT',$search[7]);
        }else if($search[8]!=''){
            $where['u.user_time'] = array('ELT',$search[8]);
        }else{
            $where['u.user_time'] = array('EGT',0);
        }
        $where['user_attestation'] = 2;
        $list = $model->alias('u')
            ->join('up_area ua1','ua1.district_id = u.user_province','left')
            ->join('up_area ua2','ua2.district_id = u.user_city','left')
            ->join('up_area ua3','ua3.district_id = u.user_area','left')
            ->where($where)
            ->order('u.user_id desc')
            ->field('u.*,ua1.district as province,ua2.district as city,ua3.district as district')
            ->select();
        return $list;
    }

    /**
 * @param $search
 * @param $whereH
 * @return false|\PDOStatement|string|\think\Collection
 * 用户统计柱状图
 */
    public function totalUser($search,$whereH){
        $model = self::instance();
        if($search[0]!=''){
            $where['u.user_province'] = array('=',$search[0]);
        }
        if($search[1]!=''){
            $where['u.user_city'] = array('=',$search[1]);
        }
        if($search[2]!=''){
            $where['u.user_area'] = array('=',$search[2]);
        }
        if($search[3]==''&&$search[4]==''){
            $time = date('Y-m-d');
            $start = date('Y-m-d',strtotime("-15 day"));
            $where['u.user_register_time'] = array('between',array($start,$time));
        } elseif($search[3] !=''  && $search[4]!=''){
            $where['u.user_register_time'] = array('between',array($search[3],$search[4]));
        }else if($search[3] !=''){
            $where['u.user_register_time'] = array('EGT',$search[3]);
        }else if($search[4]!=''){
            $where['u.user_register_time'] = array('EGT',$search[4]);
        }else{
            $where['u.user_register_time'] = array('EGT',0);
        }
        $list = $model->alias('u')
            ->group('u.user_register_time')
            ->join('up_area ua1','ua1.district_id = u.user_province','left')
            ->join('up_area ua2','ua2.district_id = u.user_city','left')
            ->join('up_area ua3','ua3.district_id = u.user_area','left')
            ->where($where)
            ->where($whereH)
            ->field('count(u.user_id) as value,u.user_register_time')
            ->select();
        return $list;
    }
    /**
     * @param $search
     * @param $whereH
     * @return false|\PDOStatement|string|\think\Collection
     * 用户统计柱状图
     */
    public function totalUserLine($search,$whereH,$whereE){
        $model = self::instance();
        $where = '';
        if($search[0]!=''){
            $where['u.user_province'] = array('=',$search[0]);
        }
        if($search[1]!=''){
            $where['u.user_city'] = array('=',$search[1]);
        }
        if($search[2]!=''){
            $where['u.user_area'] = array('=',$search[2]);
        }

        $list = $model->alias('u')
//            ->join('up_area ua1','ua1.district_id = u.user_province','left')
//            ->join('up_area ua2','ua2.district_id = u.user_city','left')
//            ->join('up_area ua3','ua3.district_id = u.user_area','left')
            ->where($where)
            ->where($whereH)
            ->where($whereE)
            ->count();
        return $list;
    }
    /**
     * @param $search
     * @param $whereH
     * @return false|\PDOStatement|string|\think\Collection
     * 用户统计柱状图表头
     */
    public function userNum($search,$whereH){
        $model = self::instance();
        $where = '';
        if($search[0]!=''){
            $where['u.user_province'] = array('=',$search[0]);
        }
        if($search[1]!=''){
            $where['u.user_city'] = array('=',$search[1]);
        }
        if($search[2]!=''){
            $where['u.user_area'] = array('=',$search[2]);
        }

        $list = $model->alias('u')
            ->where($where)
            ->where($whereH)
            ->count();
        return $list;
    }

    /**
     * @param $search
     * @param $whereH
     * @return false|\PDOStatement|string|\think\Collection
     * 用户统计柱状图表头
     */
    public function userNumTotal($search,$whereH,$whereE){
        $model = self::instance();
        $where = '';
        if($search[0]!=''){
            $where['u.user_province'] = array('=',$search[0]);
        }
        if($search[1]!=''){
            $where['u.user_city'] = array('=',$search[1]);
        }
        if($search[2]!=''){
            $where['u.user_area'] = array('=',$search[2]);
        }

        $list = $model->alias('u')
//            ->join('up_area ua1','ua1.district_id = u.user_province','left')
//            ->join('up_area ua2','ua2.district_id = u.user_city','left')
//            ->join('up_area ua3','ua3.district_id = u.user_area','left')
            ->where($where)
            ->where($whereH)
            ->where($whereE)
            ->count();
        return $list;
    }
    /**
     * @param $uid
     * @param string $field
     * @return array|false|\PDOStatement|string|\think\Model
     * 获取一条用户信息
     */
    public function getUserOne($where,$field='*'){
        $model = self::instance();
        $result = $model->where($where)->field($field)->find();
        return $result;
    }
    private static $instance;

    public static function instance()
    {
        if (!(self::$instance instanceof self)) {
            self::$instance = new self();
        }
        return self::$instance;
    }
}